<?php
class Notice{
    protected $main_table = 'data_notice';
	protected $main_id = 'notice_id';
	protected $db = null;
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	/**
	* 	insert news
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	/**
	* 	update news
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	/**
	* 	delete news
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}
	
	/**
	* 	delete photos
	* 	@param string $photos 
	*/
	public function del_photo($photos){
		if($photos){
			$array = explode(',',$photos);
			foreach($array as $photo){
				$file = UPLOAD_PHOTO.$photo;
				$file2 = UPLOAD_PHOTO.'thumb/'.$photo;
				del_file($file);
				del_file($file2);
			}
		}
	}
	
	/**
	* 	set news (hot display etc.)
	*	@param string $default 
	*	@param string $type 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function set_data($default='',$type,$where){
		if(!$type){
			my_jump('请指定更新类型');
		}
		$set = array();
		switch($type){
			case 'hot':
				if($default=='1')
				$set = array('notice_hot'=>'0');
				else
				$set = array('notice_hot'=>'1');
			break;
			case 'check':
				if($default=='1')
				$set = array('notice_check'=>'0');
				else
				$set = array('notice_check'=>'1');
			break;
			case 'show':
				if($default=='1')
				$set = array('notice_show'=>'0');
				else
				$set = array('notice_show'=>'1');
			break;
			case 'comment':
				if($default=='1')
				$set = array('notice_comment'=>'0');
				else
				$set = array('notice_comment'=>'1');
			break;
			case 'hit':
				$set = array('notice_hit'=>"notice_hit+1");
			break;
			default:
				my_jump('输入的参数有误');
			break;
		}
		$result = $this->db->update($this->main_table,$set,$where);
		return $result;
	}
	
	
	/**
	* 	get news info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id,$field='*'){
		if(!intval($id))
		my_jump('输入的参数有误!');
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$this->main_id."='".intval($id)."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	
	/**
	* 	get news array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='notice_id DESC',$limit='10',$field='notice_id,notice_title,notice_file,notice_dpt,notice_dpt_type,notice_type,notice_create_time,notice_url'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		//echo $sql;
		return $data;
	}   

	/**
	* 	get news content
	* 	@param int $id
	* 	@param int $type
	* 	@return array data
	*/
	public function get_data_content($id,$cpage='1',$controller,$type='0'){
		$check = '';
		$row = $this->get_data_row($id);
		
		if(!$_SESSION['admin_auth']){								//非管理员就不能查看非审核文章
			if($row['notice_check']!='1'){
				my_jump('你没有浏览此文章的权限!');
			}
			if($row['notice_show'] !='1'){
				my_jump('此文章已被屏蔽!');
			}
		}
		if(strstr($row['notice_content'],"<!--[page]-->")){
			$content = explode("<!--[page]-->",$row['notice_content']);
			load_file('page_class',LIB_PATH);
			$MyPage = new Mypage($db);
			$content = $MyPage->from_data($content,$cpage,1);
			$row['content'] = $content[0];
			$row['page'] = $MyPage->get_content_page($content,$controller);
		}else{
			$row['content'] = $row['notice_content'];
		}
		
		return $row;
	}

	public function get_next($id,$fid){
		$sql = "SELECT notice_id,notice_title FROM ".$this->main_table." WHERE ".$this->main_id.">'".intval($id)."' AND notice_show='1' AND notice_check='1' AND notice_type='0' AND notice_sort='".$fid."'";
		//die($sql);
		$data = $this->db->getRow($sql);
		return $data;	
	}
	
	public function get_pre($id,$fid){
		$sql = "SELECT notice_id,notice_title FROM ".$this->main_table." WHERE ".$this->main_id."<'".intval($id)."' AND notice_show='1' AND notice_check='1' AND notice_type='0' AND notice_sort='".$fid."' ORDER BY notice_id DESC";
		//die($sql);
		$data = $this->db->getRow($sql);
		return $data;	
	}

	public function get_count($where){
		$sql = "SELECT count(*) as count FROM ".$this->main_table." WHERE ".$where;
		$data = $this->db->getOne($sql);
		return $data;
	}

	/**
	* 	get data array
	* 	@param int $limit
	* 	@param string $type
	* 	@param int $fid
	* 	@param int $type2 0-news 1-seller news 2-other
	* 	@return array data
	*/
	public function get_info($limit='10',$type='new',$fid='',$type2=0){
		$add_where = intval($fid) ? " AND notice_sort=".$fid." " : ''; 
		$default_where = "notice_show='1' AND notice_check='1' AND notice_type ='".$type2."' ".$add_where;
		switch($type){
			case "new":										//最新的
				$field = 'notice_id,notice_title,notice_photo,notice_dsc,notice_sort_title,notice_create_time';
				$where = $default_where;
				$order = "notice_id DESC";
			break;
			case "hot":										//推荐
				$field = 'notice_id,notice_title,notice_photo';
				$where = $default_where." AND notice_hot='1' ";
				$order = "notice_id DESC";
			break;
			case "count":									//数量	
				$where = $default_where;	
				return $this->get_count($where);
			break;
			default:
			break;
			
		}
		$ary = $this->get_data_array($where,$order,$limit,$field);
		if($ary){
			foreach($ary as $k=>$v){
				$ary[$k]['photo'] = $v['notice_photo'] ? UPLOAD_PHOTO_URL.'thumb/'.$v['notice_photo'] : '';
				$ary[$k]['link'] = 'default/newsinfo-id_'.$v['notice_id'].'.htm';
			}
		}
		return $ary;
	}


	/**
	* 	check news if can repla
	* 	@param string $where
	* 	@return array data
	*/
	
	public function check_reply($aid,$time=30){				//$aid为文章id,$time为时效
		$check = 0;
		if($_SESSION['view']){								//记录信息
			if($_SESSION['view']['ip'] == get_ip())
			$check++;
			if($_SESSION['view']['aid']==$aid)
			$check++;
			if((time()-$_SESSION['view']['time']) < $time)
			$check++;
		}
		if($check!='3' or !$_SESSION['view']){				//不满足条件则可增加	
			$artcle['aid'] = $aid;
			$artcle['time'] = time();
			$artcle['ip'] = get_ip();
			$_SESSION['view'] = $artcle;
			//$data = array('notice_hits'=>"notice_hits + 1");
			//$result=$this->update_data($data,"notice_id='$aid'");
			return true;
		}else{
			return false;
		}
	}
  
	public function get_notice_detail($id){
	$sql = "SELECT * FROM ".$this->main_table." WHERE notice_id='".$id."'";
	//die($sql);
	$data = $this->db->getRow($sql);
	return $data;	
	}
	
	public function get_about_data($where){
		$sql = "SELECT * FROM ".$this->main_table." WHERE ".$where." Order by notice_id Limit 2";
		
		$data = $this->db->getAll($sql);
		return $data;
	} 
	public function get_qi_data($where){
		$sql = "SELECT * FROM ".$this->main_table." WHERE ".$where." AND notice_show='1'";
		
		$data = $this->db->getRow($sql);
		return $data;
	} 
	
	public function get_notice_list($where,$limit){
		$sql = "SELECT * FROM ".$this->main_table." where ".$where." ORDER BY notice_id";
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	} 
	
	public function get_hot_news($where,$limit='1'){
		$sql = "SELECT * FROM ".$this->main_table." where ".$where." ORDER BY notice_id";
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getRow($sql);
		return $data;
	} 
	

	
}

?>